#ifndef SYSTEMCONTROL_H
#define SYSTEMCONTROL_H

#include <QObject>
#include <memory>
#include <QList>
#include "system_input.h"

class Client;
class Camera;
class ImageProcess;
class ImageSave;
class DeviceFile;
class QTimer;
class SystemControl : public SystemInput
{
    Q_OBJECT

    enum PhotoMode{
        PhotoAndSave = 0x1,
        Photo
    };

public:
    explicit SystemControl(Client* client);
    ~SystemControl() override;

    //依赖注入
    void setCameraEngine(Camera* camera);
    void setImageProcessEngine(ImageProcess *image_process);
    void setImageSaveEngine(ImageSave *image_save);
    void setDeviceFileEngine(DeviceFile *device_file);

    void dataInput(QByteArray data) override;

public slots:
    //图像被捕获
    void onImageCaptured(unsigned char *image_pointer);

    //上传图像生成完成
    void onUploadImageCreateSuccess();

signals:
    //向Client发送
    void sendData(const QByteArray data);

    //向ImageProcess发送
    void sigGenerateUploadImage(const unsigned char *image_pointer);

    //向FileImage发送
    void sigAddImageAuxiliaryData(const QByteArray data);
    void sigGenerateUploadImageData(const unsigned char *image_pointer);

private:
    //数据拆分成协议格式
    int DataSplit();
    //检查收到的指令并执行
    bool checkRecvCommand(const QByteArray &cmd);
    //获取设备唯一标识
    void acquireDeviceUniqueSign();
    //相机初始化结果
    void cameraInitializationResult(int init_result);

private:
    //指令下达并发送反馈信息(下发/读取/反馈)
    int getCameraInfo(unsigned short frame);
    int setCameraMode(unsigned short frame, QByteArray data);
    int setTriggerSource(unsigned short frame, QByteArray data);
    int triggerOne(unsigned short frame);
    int setExposureTime(unsigned short frame, QByteArray data);
    int setFrame(unsigned short frame, QByteArray data);
    int setAnologGain(unsigned short frame, QByteArray data);
    int setWindow(unsigned short frame, QByteArray data);
    int getTemperature(unsigned short frame);
    int setPictureType(unsigned short frame, QByteArray data);
    int cameraRecover(unsigned short frame);
    int setSaveLocation(unsigned short frame, QByteArray data);
    int setPhotoMessage(unsigned short frame, QByteArray data);
    int setPhotoBit(unsigned short frame, QByteArray data);
    int clearStorage(unsigned short frame, QByteArray data);
    int getExposureCount(unsigned short frame);
    int heartbeatFeedback(unsigned short frame);

    //主动发送的心跳指令
    void heartbeatFeedback();
    //上传故障
    void cameraMalfunction(unsigned short frame,  unsigned char data);
    //外触发反馈
    void externalTrigger();

private:
    std::unique_ptr<Client> system_output_;
    std::unique_ptr<Camera> camera_device_;
    std::unique_ptr<ImageProcess> thumbnail_process_;
    std::unique_ptr<ImageSave> file_image_;
    std::unique_ptr<DeviceFile> device_file_;
    //收到服务器传来的所有数据+没有解析完成的数据
    QByteArray data_input_;
    //收到服务器发来的解析过的图像辅助信息
    QByteArray image_auxiliary_message_;

    //主动发送心跳指令
    QTimer *heartbeat_time_;

private:
    //标志位

    unsigned char pohot_mode_ = Photo;

    unsigned char device_number_;

    unsigned char camera_initialization_result_;

    unsigned int image_count_index_ = 0;

    unsigned char external_trigger_ = 0x2;

};

#endif // SYSTEMCONTROL_H
